package main

import "fmt"

func main() {
	r, m := "a", "b"
	v := canConstruct(r, m)
	fmt.Println(v)
}

func canConstruct1(ransomNote string, magazine string) bool {
	if len(ransomNote) > len(magazine) {
		return false
	}
	m := make(map[rune]int, len(magazine))
	for _, v := range magazine {
		if i, ok := m[v]; ok {
			m[v] = i + 1
		} else {
			m[v] = 1
		}
	}

	for _, v := range ransomNote {
		if i, ok := m[v]; ok {
			if i == 0 {
				return false
			} else {
				m[v] = i - 1
				continue
			}
		} else {
			return false
		}
	}

	return true
}

func canConstruct(ransomNote string, magazine string) bool {
	if len(ransomNote) > len(magazine) {
		return false
	}

	m := make(map[rune]int, len(magazine))
	for _, v := range magazine {
		if i, ok := m[v]; ok {
			m[v] = i + 1
		} else {
			m[v] = 1
		}
	}

	r := make(map[rune]int, len(ransomNote))
	for _, v := range ransomNote {
		if i, ok := r[v]; ok {
			r[v] = i + 1
		} else {
			r[v] = 1
		}
	}

	for k, d := range r {
		if t, ok := m[k]; ok {
			if t < d {
				return false
			}
		} else {
			return false
		}
	}

	return true
}
